package com.clouds.wms.dao;

import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Repository;
import com.clouds.wms.bean.Cmsml;
import com.clouds.wms.exception.DatabaseException;
@Repository("cmsmlDao")
public class CmsmlDaoImpl implements CmsmlDao {
	private Log log = LogFactory.getLog(getClass());
	
	@Resource(name="namedParameterJdbcTemplate")
	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
	@Override
	public List<Cmsml> getCmsmlListByMlab(String mlab) throws DatabaseException {
		String sql = "SELECT * FROM cmsml WHERE mlab=?";
		try {
			return namedParameterJdbcTemplate.getJdbcOperations().query(sql, new Object[]{mlab}, new BeanPropertyRowMapper<Cmsml>(Cmsml.class));
		} catch (Exception e) {
			log.error("exception :", e);
			throw new DatabaseException(e.getMessage());
		}
	}
	@Override
	public int[] saveCmsmls(List<Cmsml> syseList) throws DatabaseException {
		try {
			SqlParameterSource[] batchArgs = SqlParameterSourceUtils.createBatch(syseList.toArray());
			String sql = "INSERT INTO cmsml (mlab, mlac) VALUES (:mlab, :mlac)";
			return namedParameterJdbcTemplate.batchUpdate(sql, batchArgs);
		} catch(Exception e) {
			log.error(e.getMessage(), e);
			throw new DatabaseException(e.getMessage());
		}
	}
	@Override
	public int deleteCmsmlListByMlab(String mlab) throws DatabaseException {
		String sql = "DELETE FROM cmsml WHERE mlab=?";
		try {
			return namedParameterJdbcTemplate.getJdbcOperations().update(sql, new Object[]{mlab});
		} catch (Exception e) {
			log.error("exception :", e);
			throw new DatabaseException(e.getMessage());
		}
	}

}
